jQ(function(){
	jQ("a.contact_group").addClass("contact_grouponit");
	getExistGroup();
});

function getExistGroup(){
	jQ.ajax({
		type: "POST",
		url:"contactAjax!getExistGroup.action",
		success:function(data){
			var length=jQ(data).find("ContactGroup").length;
			jQ("#existgrouplist").empty();
			var html="";
			jQ(data).find("ContactGroup").each(function(){
				var groupname=jQ(this).find("groupname").text();
				var groupid=jQ(this).attr("id");
				var membercount=eval(jQ(this).find("membercount").text());
				var countstr=membercount>99?membercount+"+人":membercount+"人";
				html+="<div class=\"elementout\" groupid="+groupid+" count="+membercount+">";
				html+="<div class=\"element\">";
				html+="<span class=\"groupname\">";
					html+=groupname;
				html+="</span>";
				html+="<span class=\"groupcount\">";
					html+=countstr;
				html+="</span>";
				html+="</div>";
				html+="</div>";
			});
			jQ("#existgrouplist").append(html);
			jQ("#existgrouplist>div").unbind("mouseover").bind("mouseover",existgrouplist_mouseover);
			jQ("#existgrouplist>div").unbind("mouseout").bind("mouseout",existgrouplist_mouseout);
			jQ("#existgrouplist>div").unbind("click").bind("click",existgrouplist_click);
		}
	});
}

function existgrouplist_mouseover(){
	if(jQ(this).hasClass("elementout"))
		jQ(this).addClass("elementover");
}
function existgrouplist_mouseout(){
	if(jQ(this).hasClass("elementout"))
		jQ(this).removeClass("elementover");
}
function existgrouplist_click(){
	jQ("#existgrouplist div.elementon").removeClass("elementon").addClass("elementout");
	jQ(this).unbind("mouseover").unbind("mouseout");
	jQ(this).removeClass().addClass("elementon");
	var groupid=eval(jQ(this).attr("groupid"));
	loadGroupmembers(groupid);
	loadContactsBesidesGroups(groupid);
	jQ("#editgroupname").val(jQ(this).find("div.element").find("span.groupname").text());
	jQ("#editgroupmembers").empty().append(jQ(this).attr("count")+"人").data("count",jQ(this).attr("count"));
	jQ("#editgroupname").data("groupid",groupid);
	jQ("#editgroupname").unbind("click").bind("click",function(){
		if(!jQ(this).hasClass("edit")){
			jQ(this).addClass("edit");
		}
	});
	jQ("#editgroupname").unbind("blur").bind("blur",function(){
		if(jQ(this).hasClass("edit")){
			jQ(this).removeClass("edit");
			var groupid=eval(jQ("#editgroupname").data("groupid"));
			updateGroupName(jQ(this));
		}
	});
	jQ(".editmode").show();
}

function loadGroupmembers(groupid){
	jQ.ajax({
		type: "POST",
		url:"contactAjax!loadContactsInGroup.action",
		data:{
			groupid:eval(groupid)
		},
		success:function(data){
			createGroupMembersHtml(data,groupid);
		}
	});
	
}

function effectGroupMembers(){
	
	jQ("#groupeditlist div.gelement").unbind("click").bind("click",function(){
		if(jQ(this).hasClass("gelementon")){
			jQ(this).removeClass("gelementon");
		}else{
			jQ(this).addClass("gelementon");
		}
		
	});
	
}

function effectContactmembers(){
	jQ("#contacteditlist div.gelement").unbind("click").bind("click",function(){
		if(jQ(this).hasClass("gelementon")){
			jQ(this).removeClass("gelementon");
		}else{
			jQ(this).addClass("gelementon");
		}
		
	});
	
}

function addnewgroup(obj){
	var _input=jQ(obj).prev();
	var val=jQ.trim(_input.val());
	if(val.length>15){
		jQ(obj).next("span.errorinfo").empty().append("名称不能超过15个字。");
	}else if(val.length<=0){
		jQ(obj).next("span.errorinfo").empty().append("名称不能为空。");
	}else{
		jQ.ajax({
			type: "POST",
			url:"contactAjax!createContactGroup.action",
			data:{
				groupname:val
			},
			success:function(data){
				var dataObj=eval("("+data+")");
				
				if(dataObj.flag){
					_input.val("");
					var groupid=dataObj.groupid;
					var html="";
					html+="<div class=\"elementon\" groupid="+groupid+" count=0>";
					html+="<div class=\"element\">";
					html+="<span class=\"groupname\">";
						html+=val;
					html+="</span>";
					html+="</div>";
					html+="</div>";
					jQ("#existgrouplist>div").removeClass().addClass("elementout");
					jQ("#existgrouplist").append(html);
					jQ("#editgroupname").val(val);
					jQ("#editgroupmembers").data("count",0);
					jQ("#editgroupmembers").empty().append("0人");
					jQ("#editgroupname").data("groupid",groupid);
					jQ("#editgroupname").unbind("click").bind("click",function(){
						if(!jQ(this).hasClass("edit")){
							jQ(this).addClass("edit");
						}
					});
					jQ("#editgroupname").unbind("blur").bind("blur",function(){
						if(jQ(this).hasClass("edit")){
							updateGroupName();
						}
					});
					jQ("#existgrouplist>div").unbind("mouseover").bind("mouseover",existgrouplist_mouseover);
					jQ("#existgrouplist>div").unbind("mouseout").bind("mouseout",existgrouplist_mouseout);
					jQ("#existgrouplist>div").unbind("click").bind("click",existgrouplist_click);
					jQ("#groupeditlist").empty();
					jQ(".editmode").show();
					loadContactsBesidesGroups(groupid);
				}else{
					var msg=dataObj.msg;
					jQ(obj).next("span.errorinfo").empty().append(msg);
				}
			}
		});
		
	}
	
}

function updateGroupName(){
	var val=jQ.trim(jQ("#editgroupname").val());
	if(val.length>15){
		jQ("#editgroupname").nextAll("span.updatenameerr").empty().append("名称不能超过15个字。");
	}else if(val.length<=0){
		jQ("#editgroupname").nextAll("span.updatenameerr").empty().append("名称不能为空。");
	}else if(val==jQ("#editgroupname").attr("title")){
		jQ("#editgroupname").val(val);
		return;
	}else{
		var groupid=eval(jQ("#editgroupname").data("groupid"));
		jQ.ajax({
			type: "POST",
			url:"contactAjax!updateGroupName.action",
			data:{
				groupid:groupid,
				groupname:val
			},
			success:function(data){
				var dataObj=eval("("+data+")");
				if(dataObj.flag){
					jQ("#editgroupname").val(val);
					jQ("#editgroupname").removeClass("edit");
					jQ("#editgroupname").attr("title",val);
					jQ("#existgrouplist div[groupid="+groupid+"]").find("div.element").find("span.groupname").empty().append(val);
				}else{
					jQ("#editgroupname").nextAll("span.updatenameerr").empty().append(dataObj.msg);
				}
			}
		});
	}
}

function loadContactsBesidesGroups(groupid){
	var url="";
	var usertype=eval(jQ("#var_usertype").val());
	if(usertype==1){
		url="contactAjax!loadContactsBesidesGroup.action";
	}else{
		url="contactAjax!loadSubscribesBesidesGroup.action";
	}
	jQ.ajax({
		type: "POST",
		url:url,
		data:{
			groupid:eval(groupid)
		},
		success:function(data){
			createContactHtml(data);
		}
	});
}

function createContactHtml(data){
	
	jQ("#contacteditlist").empty();
	var html="";
	var contactlength=jQ(data).find("Contact").length;
	jQ(data).find("Contact").each(function(){
		var _id = jQ(this).find("id").text();
		var _name = jQ(this).find("name").text();
		var _email = jQ(this).find("email").text();
		var _uid=jQ(this).find("uid").text();
		var _ulogo=jQ(this).find("ulogo").text();
		html+="<div class=\"gelement\" uid="+_uid+">";
		html+="<div class=\"top\">";
			html+="<span class=\"name\">"+_name+"</span>";
			html+="<img src=\""+_ulogo+"\">";
		html+="</div>";
		html+="<div class=\"bottom\">";
			html+=_email;
		html+="</div>";
		html+="</div>";
	});
	jQ("#contacteditlist").append(html);
	effectContactmembers();
}

function createGroupMembersHtml(data,groupid){
	jQ("#groupeditlist").empty();
	var html="";
	var contactlength=jQ(data).find("Contact").length;
	jQ("#editgroupmembers").empty().append(contactlength+"人");
	jQ("#existgrouplist>div[groupid="+groupid+"]").attr("count",contactlength);
	jQ(data).find("Contact").each(function(){
		var _id = jQ(this).find("id").text();
		var _name = jQ(this).find("name").text();
		var _email = jQ(this).find("email").text();
		var _uid=jQ(this).find("uid").text();
		var _ulogo=jQ(this).find("ulogo").text();
		html+="<div class=\"gelement\" uid="+_uid+">";
		html+="<div class=\"top\">";
			html+="<span class=\"name\">"+_name+"</span>";
			html+="<img src=\""+_ulogo+"\">";
		html+="</div>";
		html+="<div class=\"bottom\">";
			html+=_email;
		html+="</div>";
		html+="</div>";
	});
	jQ("#groupeditlist").append(html);
	effectGroupMembers();
	
}

function addContacts2Group(){
	var contact="";
	var addcount=jQ("#contacteditlist div.gelementon").length;
	if(addcount==0)
		return;
	jQ("#contacteditlist div.gelementon").each(function(i){
		var uid=jQ(this).attr("uid");
		if(i==0){
			contact+=uid;
		}else{
			contact+=","+uid;
		}
	});
	var groupid=eval(jQ("#editgroupname").data("groupid"));
	jQ.ajax({
		type: "POST",
		url:"contactAjax!addContacts2Group.action",
		data:{
			groupid:groupid,
			contacts:contact
		},
		success:function(data){
			var dataObj=eval("("+data+")");
			
			if(dataObj.flag){
				var count=eval(jQ("#editgroupmembers").data("count"));
				count=count+addcount;
				jQ("#existgrouplist div[groupid="+groupid+"]").attr("count",count);
				jQ("#existgrouplist div[groupid="+groupid+"]").find("div.element").find("span.groupcount").empty().append(count+"人");
				jQ("#editgroupmembers").empty().append(count+"人").data("count",count);
				jQ("#contacteditlist div.gelementon").each(function(){
					var html=jQ(this).html();
					var uid=jQ(this).attr("uid");
					jQ("#groupeditlist").append("<div uid="+uid+" class='gelement'>"+html+"</div>");
					jQ(this).remove();
					effectGroupMembers();
				});
				
			}
		}
	});
}

function removeContactsFromGroup(){
	var contact="";
	var removecount=jQ("#groupeditlist div.gelementon").length;
	if(removecount==0)
		return;
	jQ("#groupeditlist div.gelementon").each(function(i){
		var uid=jQ(this).attr("uid");
		if(i==0){
			contact+=uid;
		}else{
			contact+=","+uid;
		}
	});
	var groupid=eval(jQ("#editgroupname").data("groupid"));
	jQ.ajax({
		type: "POST",
		url:"contactAjax!removeContactsFromGroup.action",
		data:{
			groupid:groupid,
			contacts:contact
		},
		success:function(data){
			var dataObj=eval("("+data+")");
			
			if(dataObj.flag){
				var count=eval(jQ("#editgroupmembers").data("count"));
				count=count-removecount;
				count=count<0?0:count;
				jQ("#existgrouplist div[groupid="+groupid+"]").attr("count",count);
				jQ("#existgrouplist div[groupid="+groupid+"]").find("div.element").find("span.groupcount").empty().append(count+"人");
				jQ("#editgroupmembers").empty().append(count+"人").data("count",count);
				jQ("#groupeditlist div.gelementon").each(function(){
					var html=jQ(this).html();
					var uid=jQ(this).attr("uid");
					jQ("#contacteditlist").append("<div uid="+uid+" class='gelement'>"+html+"</div>");
					jQ(this).remove();
					effectContactmembers();
				});
				
			}
		}
	});
}

function deletegroup(){
	jalert("确定要删除？",null,deletegroupevent);
}

function deletegroupevent(){
	var groupid=eval(jQ("#editgroupname").data("groupid"));
	jQ.ajax({
		type: "POST",
		url:"contactAjax!deleteContactGroup.action",
		data:{
			groupid:groupid
		},
		success:function(data){
			var dataObj=eval("("+data+")");
			if(dataObj.flag){
				jQ("#existgrouplist div[groupid="+groupid+"]").remove();
				jQ("#existgrouplist>div").removeClass().addClass("elementout");
				jQ("#groupeditlist").empty();
				jQ(".editmode").hide();
			}else{
				clip_tips(dataObj.msg);
			}
		}
	});
}

function closegroup(){
	jQ("#existgrouplist>div").removeClass().addClass("elementout");
	jQ("#groupeditlist").empty();
	jQ(".editmode").hide();
}


function checkStatus(obj,event) {
	if(event.type=='focus'){
		if(jQ(obj).hasClass("default")){
			jQ(obj).removeClass("default").addClass("onit");
			jQ(obj).val("");
		}
	}else if(event.type=='blur'){
		if(jQ(obj).hasClass("onit")){
			if(jQ.trim(jQ(obj).val()).length==0){
				jQ(obj).removeClass("onit").addClass("default");
				if(eval(jQ("#search_type_sel").val())==1)
					jQ(obj).val("输入搜索的真实姓名");
				else
					jQ(obj).val("输入搜索的订阅商家名");
			}
		}
	}
}

function changeUserType(obj){
	var val=eval(jQ(obj).val());
	if(val==1){
		jQ("#searchinput").val("输入搜索的真实姓名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("default");
	}else{
		jQ("#searchinput").val("输入搜索的订阅商家名");
		jQ("#searchinput").removeClass().addClass("add_ul_input").addClass("default");
	}
}

function searchUser(obj){
	var inputobj=jQ(obj).prev("input");
	var val=jQ.trim(inputobj.val());
	if(inputobj.hasClass("default"))
		val="";
	var tm= encodeURI(val);
	tm=encodeURI(tm);
	var searchtype=jQ("#search_type_sel").val();
	if(val.length==0){
		if(eval(searchtype)==1)
			jQ("#search_info").empty().append("输入您要搜索的档期用户。");
		else
			jQ("#search_info").empty().append("输入您要搜索的订阅商家名。");
		return;
	}else{
		var tm= encodeURI(val);
		tm=encodeURI(tm);
		window.location.href=jQ("#ctxVal").val()+"/contact/search.html?type="+searchtype+"&content="+tm;
	}
}